var sys = new Object();
sys.enterPressed = 0;
sys.timeNow = new Date().getTime();
sys.enableAutoUpdate = true;

function pausecomp(ms) {
	ms += new Date().getTime();
	while (new Date() < ms){}
} 

$(document).ready(function(){
	$.ajaxSetup({
	    'beforeSend' : function(xhr) {
	        xhr.overrideMimeType('text/html; charset=UTF-8');
	    },
	});
/*
	$(".detect_click").click(function () {
		var tid= $(this).attr('rel');
		if( $('#'+tid).css('display') == 'none' ){
			$.post( "index2.php",{action:'user',sub:'show_comments',tid:tid},function(response){
				$('#'+tid).html(response).show();
				var number = $("#"+tid+" .comment").size()-1;
				$('a.detect_click[rel="'+tid+'"]').html('<img alt="comments" src="./img/comments.png"> '+number);
			});
		}else{
			$('#'+tid).hide();
		} 
	}); 
*/	 
	if($('#latest_workout').length){
		setInterval('get_workout()',30000);
	}
})

function get_workout(){
	var timeDelay_ms = 5000;
	var timeNow = new Date().getTime();
	
	if(sys.timeNow + timeDelay_ms> timeNow ){
		//pausecomp(timeDelay_ms);
	}
	if(sys.enableAutoUpdate){
		$('#latest_workout').load('index2.php?action=workout&sub=latest_show&show='+sys.action.show); 
	}	
	//$('#latest_workout').slideDown(500);
	sys.timeNow = new Date().getTime();
}

function redirect_to(val){
	window.location = "index.php?action=home&show="+val;
}

function open_comments($training_id, $owner_id){
	sys.timeNow = new Date().getTime();
	
	var tid= $training_id;
	if( $('#'+tid).css('display') == 'none' ){
		$.post( "index2.php",{action:'user',sub:'show_comments',tid:tid,owner_id:$owner_id},function(response){
			$('#'+tid).html(response).show();
			var number = $("#"+tid+" .comment").size()-1;
			$('a.detect_click[rel="'+tid+'"]').html('<img alt="comments" src="./img/comments.png"> '+number);
		});
	}else{
		$('#'+tid).hide();
	} 	
}

function open_comments2($training_id, $owner_id){
	sys.timeNow = new Date().getTime();
	
	$.post( "index2.php",{action:'user',sub:'show_comments',tid:$training_id,owner_id:$owner_id},function(response){
		$('#'+$training_id).empty();
		$('#'+$training_id).html(response).show();
	});	
}

function comment($this, $owner_id, $tid,e){ 
	//sys.timeNow = new Date().getTime();
	sys.enableAutoUpdate = false;
	
	var code = (e.keyCode ? e.keyCode : e.which);
	if(sys.enterPressed != 0){return}
 	if(code == 13) { //Enter keycode
 		sys.enterPressed = 1;
 		e.preventDefault();		
		$.post( "index2.php",{action:'user',sub:'insert_comment',tid:$tid,comment:$this.value,owner_id:$owner_id},function(response){
			$.post( "index2.php",{action:'user',sub:'show_comments',tid:$tid,owner_id:$owner_id},function(response){
				$('#'+$tid).html(response).show();
				var number = $("#"+$tid+" .comment").size()-1;
				$('a.detect_click[rel="'+$tid+'"]').html('<img alt="comments" src="./img/comments.png"> '+number);
				
				sys.enterPressed = 0;
				sys.enableAutoUpdate = true;
				//get_workout();
			});
		});
	}	
}

function like_unlike($status, $my_id, $owner_id, $training_id){
	$.post("index2.php",{action:'user',sub:'like_unlike',status:$status,my_id:$my_id,owner_id:$owner_id,training_id:$training_id},function(response){
		$('span.like[rel="'+$training_id+'"]').html(response);
	});	
	get_workout();
}

function like_unlike_comment($status, $my_id, $owner_id, $training_id, $comment_id){
	$.post(
		"index2.php",
		{
			action:'user',
			sub:'like_unlike',
			status:$status,
			my_id:$my_id,
			owner_id:$owner_id,
			training_id:$training_id,
			comment_id:$comment_id
		},
		function(response){
			$('span.like_comment[rel="'+$comment_id+'"]').html(response);
		}
	);	
//	get_workout();
}

function open_who_likes($training_id){
	$.post(
		"index2.php",
		{	
			action:'user',
			sub:'like_unlike',
			tid:$training_id,
			status:'show_users_like_workout'
		}, 
		function(res){
			$('#like_popup_view').html(res);
			$("#like_popup_view").dialog(
				{ 
					dialogClass: 'dialog',
					width:550,
					buttons: [
					   {
						   text: "OK",
						   click: function(){ 
								$(this).dialog("close");
						   }
					   }
					] 
				}
			);
		}
	);
}

function open_who_like_comment($training_id,$comment_id){ 
	$.post(
		"index2.php",
		{	
			action:'user',
			sub:'like_unlike',
			tid:$training_id,
			comment_id:$comment_id,
			status:'show_users_like_comment'
		}, 
		function(res){
			$('#like_popup_view').html(res);
			$("#like_popup_view").dialog(
				{ 
					dialogClass: 'dialog',
					width:550,
					buttons: [
					   {
						   text: "OK",
						   click: function(){ 
								$(this).dialog("close");
						   }
					   }
					] 
				}
			);
		}
	);
}

function follow_this_user($user_id, $div_id){
	$.post(
		"index2.php",
		{	
			action:'user',
			sub:'like_unlike',
			user_id:$user_id,
			status:'following_user'
		},
		function(response){
			$('span.follow_user[rel="'+$div_id+'"]').html(response);
		}
	);
}




















function choose_ex_kind(){
	$.post("index2.php",{action:'user',sub:'edit_exercise',cat_id:sys.cat_id,task:'update_ex_selection'}, function(res){
		$('#select_exercise').html(res);
	});
}
function changed_cat_ex(value){
	$.post("index2.php",{action:'user',sub:'edit_exercise',cat_id:value,task:'change_ex_slb'}, function(res){
		$('#ex_div').html(res);
	});
}

function change_mode_display($this){
	if($this.checked){ // use selectbox
		$('#select_category').show();
		$('#select_exercise1').show();
		$('#select_category2').hide();
		$('#select_exercise2').hide();
		
		$('#mod_display').attr('value','slb');
	}else{	// use auto complete
		$('#select_category').hide();
		$('#select_category2').show();
		$('#select_exercise1').hide();
		$('#select_exercise2').show();
		
		$('#mod_display').attr('value','');
	}
}

function selected_exercise(value){
	$('#select_exercise').attr('value',value);
	$('#mod_display').attr('value','slb');
}
